<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author LYS
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="default" @click="handleExport()">
          <Icon icon="i-ant-design:download-outlined" />
          {{ t('导出') }}
        </a-button>
        <!--        <a-button type="default" @click="handleImport()">-->
        <!--          <Icon icon="i-ant-design:upload-outlined" /> {{ t('导入') }}-->
        <!--        </a-button>-->
        <a-button type="primary" @click="handleForm({})" v-auth="'cardd:kped:carddKped:edit'">
          <Icon icon="i-fluent:add-12-filled" />
          {{ t('新增') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ id: record.id })">
          {{ record.kpedKpdw }}
        </a>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <KpInputForm @register="registerKpDrawer" @success="handleSuccess" />
    <FormImport @register="registerImportModal" @success="handleSuccess" />
  </div>
</template>
<script lang="ts" setup name="ViewsCarddKpedCarddKpedList">
  import { unref } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useGlobSetting } from '/@/hooks/setting';
  import { downloadByUrl } from '/@/utils/file/download';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicColumn, BasicTable, useTable } from '/@/components/Table';
  import {
    carddKpedDelete,
    carddKpedDisable,
    carddKpedEnable,
    carddKpedListData,
  } from '/@/api/cardd/kped/carddKped';
  import { useDrawer } from '/@/components/Drawer';
  import { useModal } from '/@/components/Modal';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import FormImport from './formImport.vue';
  import KpInputForm from './kpform.vue';
  import { dateUtil } from '/@/utils/dateUtil';

  const { t } = useI18n('cardd.kped.carddKped');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);

  const getTitle = {
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: meta.title || t('开票额度管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('开票单位'),
        field: 'kpedKpdw',
        component: 'Input',
      },
      {
        label: t('开票年度'),
        field: 'kpedKpnd',
        component: 'DatePicker',
        componentProps: {
          picker: 'year',
          valueFormat: 'YYYY',
        },
      },
      {
        label: t('开票类型'),
        field: 'kpedKplx',
        component: 'Select',
        componentProps: {
          dictType: 'car_kp_type',
          allowClear: true,
        },
      },
      {
        label: t('开票季度'),
        field: 'kpedKpyj',
        component: 'Input',
      },
      // {
      //   label: t('月份'),
      //   field: 'kpedYf',
      //   component: 'Input',
      // },
      // {
      //   label: t('备注'),
      //   field: 'remarks',
      //   component: 'Input',
      // },
      {
        label: t('状态'),
        field: 'status',
        component: 'Select',
        componentProps: {
          dictType: 'sys_search_status',
          allowClear: true,
          onChange: handleSuccess,
        },
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('开票单位'),
      dataIndex: 'kpedKpdw',
      key: 'a.kped_kpdw',
      sorter: true,
      width: 230,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('开票年度'),
      dataIndex: 'kpedKpnd',
      key: 'a.kped_kpnd',
      sorter: true,
      width: 130,
      align: 'left',
      format: (text: string, record: Recordable, index: number) => {
        return dateUtil(text).format('YYYY') + '年';
      },
    },
    {
      title: t('开票类型'),
      dataIndex: 'kpedKplx',
      key: 'a.kped_kplx',
      sorter: true,
      width: 130,
      align: 'center',
      dictType: 'car_kp_type',
    },
    {
      title: t('开票月季'),
      dataIndex: 'kpedKpyj',
      key: 'a.kped_kpyj',
      sorter: true,
      width: 130,
      align: 'center',
      dictType: 'car_kp_zq',
    },
    {
      title: t('开票周期'),
      dataIndex: 'kpedZqName',
      key: 'a.kped_kpzq',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('金额'),
      dataIndex: 'kpedJe',
      key: 'a.kped_je',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('已开金额'),
      dataIndex: 'kpedYkje',
      key: 'a.kped_ykje',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('剩余金额'),
      dataIndex: 'syje',
      sorter: false,
      width: 130,
      align: 'right',
    },
    {
      title: t('税额'),
      dataIndex: 'kpedSe',
      key: 'a.kped_se',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('备注'),
      dataIndex: 'remarks',
      key: 'a.remarks',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('状态'),
      dataIndex: 'status',
      key: 'a.status',
      sorter: true,
      width: 130,
      align: 'center',
      dictType: 'sys_search_status',
    },
    {
      title: t('更新时间'),
      dataIndex: 'updateDate',
      key: 'a.update_date',
      sorter: true,
      width: 130,
      align: 'center',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'i-clarity:note-edit-line',
        title: t('编辑开票额度'),
        onClick: handleForm.bind(this, { id: record.id }),
        auth: 'cardd:kped:carddKped:edit',
      },
      {
        icon: 'i-ant-design:file-text-outlined',
        color: 'success',
        title: t('开票'),
        onClick: handleKpForm.bind(this, { kpmxEdid: record.id, syje: record.syje }),
        auth: 'cardd:kped:carddKped:edit',
        ifShow: () => record.status === '0',
      },
      {
        icon: 'i-ant-design:stop-outlined',
        color: 'error',
        title: t('停用开票额度'),
        popConfirm: {
          title: t('是否确认停用开票额度'),
          confirm: handleDisable.bind(this, record),
        },
        auth: 'cardd:kped:carddKped:edit',
        ifShow: () => record.status === '0',
      },
      {
        icon: 'i-ant-design:check-circle-outlined',
        color: 'success',
        title: t('启用开票额度'),
        popConfirm: {
          title: t('是否确认启用开票额度'),
          confirm: handleEnable.bind(this, record),
        },
        auth: 'cardd:kped:carddKped:edit',
        ifShow: () => record.status === '2',
      },
      {
        icon: 'i-ant-design:delete-outlined',
        color: 'error',
        title: t('删除开票额度'),
        popConfirm: {
          title: t('是否确认删除开票额度'),
          confirm: handleDelete.bind(this, record),
        },
        auth: 'cardd:kped:carddKped:edit',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerKpDrawer, { openDrawer: openKpDrawer }] = useDrawer();
  const [registerTable, { reload, getForm }] = useTable({
    api: carddKpedListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  function handleKpForm(record: Recordable) {
    openKpDrawer(true, record);
  }

  async function handleExport() {
    const { ctxAdminPath } = useGlobSetting();
    downloadByUrl({
      url: ctxAdminPath + '/cardd/kped/carddKped/exportData',
      params: getForm().getFieldsValue(),
    });
  }

  const [registerImportModal, { openModal: importModal }] = useModal();

  // function handleImport() {
  //   importModal(true, {});
  // }

  async function handleDisable(record: Recordable) {
    const params = { id: record.id };
    const res = await carddKpedDisable(params);
    showMessage(res.message);
    handleSuccess(record);
  }

  async function handleEnable(record: Recordable) {
    const params = { id: record.id };
    const res = await carddKpedEnable(params);
    showMessage(res.message);
    handleSuccess(record);
  }

  async function handleDelete(record: Recordable) {
    const params = { id: record.id };
    const res = await carddKpedDelete(params);
    showMessage(res.message);
    handleSuccess(record);
  }

  function handleSuccess(record: Recordable) {
    reload({ record });
  }
</script>
